<template>
  <div>
    <el-card style="margin:20px 0">
      <CategorySelect :show="scene!==0" @select="select" />
    </el-card>

    <el-card>
      <!-- 初始页面 -->
      <div v-show="scene===0">
        <el-button v-show="$store.state.user.buttons.indexOf('btn.Spu.add')!==-1" type="primary" :disabled="category3Id===''" style="margin:10px 0" icon="el-icon-plus" @click="addSpu">添加SPU</el-button>
        <el-table style="width: 100%" border :data="data">
          <template slot="empty">
            <el-empty description="空" />
          </template>
          <el-table-column
            prop="prop"
            label="序号"
            width="80px"
            align="center"
            type="index"
          />
          <el-table-column
            label="SPU名称"
            width="width"
            prop="spuName"
          />
          <el-table-column
            prop="description"
            label="SPU描述"
            width="width"
          />
          <el-table-column
            prop="prop"
            label="操作"
            width="width"
          >
            <template slot-scope="{row}">
              <div>
                <HintButton type="success" icon="el-icon-plus" :disabled="$store.state.user.buttons.indexOf('btn.Spu.addsku')===-1" size="mini" title="添加SKU" @click="add(row)" />
                <HintButton type="warning" :disabled="$store.state.user.buttons.indexOf('btn.Spu.update')===-1" icon="el-icon-edit" size="mini" title="修改SPU" @click="edit(row)" />
                <HintButton type="info" :disabled="$store.state.user.buttons.indexOf('btn.Spu.skus')===-1" icon="el-icon-info" size="mini" title="查看SKU列表" @click="handler(row)" />
                <el-popconfirm :title="`确定要删除${row.spuName}吗`" @confirm="del(row)">
                  <HintButton slot="reference" :disabled="$store.state.user.buttons.indexOf('btn.Spu.delete')===-1" type="danger" icon="el-icon-delete" size="mini" title="删除SPU" />
                </el-popconfirm>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-pagination
          :page-size="5"
          :current-page="page"
          :page-sizes="[5,10,15]"
          layout="prev,pager,next,jumper,->,sizes,total"
          style="margin-top:20px;text-align:center"
          :total="total"
          @size-change="sizeChange"
          @current-change="currentChange"
        />
      </div>

      <!-- 添加或者修改 -->
      <SpuForm v-show="scene===1" ref="SpuForm" />
      <SkuForm v-show="scene===2" ref="SkuForm" />
    </el-card>

    <el-dialog
      :title="spu.spuName"
      :visible.sync="dialogVisible"
      width="70%"
      :before-close="close"
    >
      <el-table
        v-loading="loading"
        style="width: 100%"
        :data="skuList"
        border
      >
        <template slot="empty">
          <el-empty description="空" />
        </template>
        <el-table-column
          prop="skuName"
          label="名称"
          width="width"
        />
        <el-table-column
          prop="price"
          label="价格"
          width="width"
        />
        <el-table-column
          prop="weight"
          label="重量"
          width="width"
        />
        <el-table-column
          label="默认图片"
          width="width"
          align="center"
        >
          <template slot-scope="{row}">
            <div>
              <el-image
                style="width: 100px; height: 100px"
                :src="row.skuDefaultImg"
                fit="fit"
              />
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import SpuForm from './SpuFrom'
import SkuForm from './SkuFrom'
export default {
  name: 'Spu',
  components: {
    SpuForm,
    SkuForm
  },
  data() {
    return {
      category1Id: '',
      category2Id: '',
      category3Id: '',
      page: 1,
      limit: 5,
      data: [],
      total: 0,
      scene: 0, // 默认展示初始页面
      dialogVisible: false,
      spu: {},
      skuList: [],
      loading: true
    }
  },
  methods: {
    select({ categoryId, level }) {
      if (level === 1) {
        this.category1Id = categoryId
        this.category2Id = ''
        this.category3Id = ''
      } else if (level === 2) {
        this.category2Id = categoryId
        this.category3Id = ''
      } else if (level === 3) {
        this.category3Id = categoryId
        this.page = 1
        this.limit = 5
        this.getData()
      }
    },
    // 获取列表数据
    async getData() {
      const res = await this.$api.spu.product(this.page, this.limit, this.category3Id)
      if (res.code === 200) {
        this.data = res.data.records
        this.total = res.data.total
      }
    },
    // 单页数量改变
    sizeChange(e) {
      this.limit = e
      this.getData()
    },
    // 页码改变
    currentChange(e) {
      this.page = e
      this.getData()
    },
    // 添加SKU
    add(e) {
      this.scene = 2
      this.$refs.SkuForm.getData(e, this.category1Id, this.category2Id, this.category3Id)
    },
    // 添加spu
    addSpu(e) {
      this.scene = 1
      this.$refs.SpuForm.getSpuData(e.id)
    },
    // 修改a
    edit(e) {
      this.scene = 1
      // 使用ref触发子组件中的方法
      this.$refs.SpuForm.getData(e.id)
    },
    // 删除记录
    async del(row) {
      const res = await this.$api.spu.deleteSpu(row.id)
      if (res.code === 200) {
        this.$message({ type: 'success', message: '删除成功' })
        if (this.data.length > 1) {
          // eslint-disable-next-line no-self-assign
          this.page = this.page
        } else {
          this.page = this.page - 1
        }
        if (this.page === 0) {
          this.page = 1
        }
        this.getData()
      }
    },
    // 查看sku列表
    async handler(row) {
      this.loading = true
      this.dialogVisible = true
      this.spu = row
      const res = await this.$api.spu.findBySpuId(row.id)
      if (res.code === 200) {
        this.skuList = res.data
        this.loading = false
      }
    },
    // 关闭弹窗
    close(done) {
      this.skuList = []
      done()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
